package com.aiju.cash.ui.good.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiju.bean.GoodsType;
import com.aiju.cash.R;

import java.util.List;

/**
 * @author: 佐为
 * @date: 2016-11-16 14:06
 * @version: V1.0
 * @Description: 描述
 */
public class GoodCategoryAdapter extends BaseAdapter {

    private boolean showMenu = false;
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<GoodsType> mGoodCategories;

    public GoodCategoryAdapter(Context context, List<GoodsType> goodCategories) {
        this.mContext = context;
        this.mLayoutInflater = LayoutInflater.from(context);
        this.mGoodCategories = goodCategories;
    }

    public void setShowMenu(boolean show) {
        this.showMenu = show;
    }

    @Override
    public int getCount() {
        return mGoodCategories.size();
    }

    @Override
    public Object getItem(int position) {
        return mGoodCategories.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.item_main_custom, parent,false);
            holder = new ViewHolder();
            holder.edit = (ImageView) convertView.findViewById(R.id.edit1);
            holder.delete = (ImageView) convertView.findViewById(R.id.delete1);
            holder.name = (TextView) convertView.findViewById(R.id.name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final GoodsType type = mGoodCategories.get(position);
        holder.name.setText(type.getName());
        if (!type.getName().equals("默认分类")) {
            if (showMenu) {
                holder.edit.setVisibility(View.VISIBLE);
                holder.delete.setVisibility(View.VISIBLE);
                holder.edit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnCategoryClick != null) {
                            mOnCategoryClick.onEdit(type);
                        }
                    }
                });
                holder.delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnCategoryClick != null) {
                            mOnCategoryClick.onDelete(type);
                        }
                    }
                });
            } else {
                holder.edit.setVisibility(View.GONE);
                holder.delete.setVisibility(View.GONE);
            }
        }
        return convertView;
    }

    static class ViewHolder {
        TextView name;
        ImageView delete;
        ImageView edit;
    }

    public void setOnCategoryClick(OnCategoryClick onCategoryClick) {
        this.mOnCategoryClick = onCategoryClick;
    }

    public OnCategoryClick mOnCategoryClick;

    public interface OnCategoryClick {
        void onDelete(GoodsType goodsType);

        void onEdit(GoodsType goodsType);
    }
}
